package middleware

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/common/tenant_db"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

func TenantDb() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Set(authorize.TENANT_CODE_KEY, c.GetHeader(authorize.TENANT_CODE_KEY))
		db := (&tenant_db.TenantDB{}).GetTenantDB(c)
		if db == nil {
			stark.Logger.Errorf(c, "middleware tenant_code:%s, 获取租户链接失败", c.GetHeader(authorize.TENANT_CODE_KEY))
			ErrResponse(c, ykerrcode.ACCESS_DENIED, "租户code不存在")
			c.Abort()
			return
		}
		c.Next()
	}
}
